
import java.sql.*;
import java.util.*;
import java.io.*;

public class JDBCExample {
    public static void main(String args[]) {

        try {
            Properties p = new Properties();

            try {
                //p.load(new FileInputStream("DBConfig.txt"));
                p.loadFromXML(new FileInputStream("DbConfig.xml"));
            }
            catch(IOException ioe){
                ioe.printStackTrace();
                System.out.println("Failed to open config file...");
                System.exit(0);
            }

            // Load the Driver
            String driver = p.getProperty("dbDriver");
            Class.forName(driver);
            System.out.println("LOADED DRIVER  ---> " + driver);
            
            // Create the "url"
            // assume database server is running on the localhost
            String url      = p.getProperty("connUrl");
            String username = p.getProperty("userName");
            String password = p.getProperty("passWord");

            // Connect to the database represented by url
            // with username (no username) and password (no password)
            Connection con =
                    DriverManager.getConnection(url, username, password);
            System.out.println("CONNECTED TO   ---> "+ url);
            
            // Use the Connection to create a Statement object
            Statement stmt = con.createStatement();
            
            // Execute query using Statement, receive the ResultSet
            String qry ="SELECT * FROM Customer ORDER BY cust_name";
            ResultSet rs = stmt.executeQuery(qry);
            System.out.println("EXECUTED QUERY ---> " + qry);
            
            // Print the results, row by row
            System.out.println("\nPROCESSING RESULTS:\n");
            while (rs.next()) {
                System.out.println("  SSN    : " +
                        rs.getString("ssn").trim());
                System.out.println("  NAME   : " +
                        rs.getString("cust_name").trim());
                System.out.println("  ADDRESS: " +
                        rs.getString("address").trim());
                System.out.println("");
            }
            rs.close();
            stmt.close();
            con.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
